package com.zpark.drug.service.impl;

import com.zpark.drug.mapper.UserMapper;
import com.zpark.drug.pojo.User;
import com.zpark.drug.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(String username, String password) {
        Example example = new Example(User.class);
        example.createCriteria().andEqualTo("username", username).andEqualTo("password", password);
        return userMapper.selectOneByExample(example);
    }

    @Override
    public boolean saveUser(User user) {
        return userMapper.insert(user) > 0;
    }

    @Override
    public boolean updateUserPassword(Integer userId, String newPassword) {
        User user = new User();
        user.setId(userId);
        user.setPassword(newPassword);
        return userMapper.updateByPrimaryKeySelective(user) > 0;
    }

    @Override
    public User findByEmail(String email) {
        Example example = new Example(User.class);
        example.createCriteria().andEqualTo("email", email);
        return userMapper.selectOneByExample(example);
    }

    @Override
    public boolean deleteUser(Integer userId) {
        return userMapper.deleteByPrimaryKey(userId) > 0;
    }

}
